package com.example.yunhailutong.zhongtan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.example.yunhailutong.zhongtan.Adapter.ZhdjListViewAdapter;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.MessageDetailsActivity;
import com.example.yunhailutong.zhongtan.activity.MessageListActivity;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.ZhdjEntity;
import com.example.yunhailutong.zhongtan.entity.ZhdjEntity.DataBean.ZhdjInfoBean;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.weight.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class DangJianFragment extends BaseFragment {
    View view = null;


    @BindView(R.id.zhdj_dyjy_imageview)
    RoundImageView iv_dyjy ;
    @BindView(R.id.zhdj_dygl_imageview)
    RoundImageView iv_dygl;
    @BindView(R.id.zhdj_dyfw_imageview)
    RoundImageView  iv_dyfw;
    @BindView(R.id.zhdj_dyfc_imageview)
    RoundImageView iv_dyfc ;



    @BindView(R.id.zhdj_top_imageview)
    RoundImageView iv_top;
    @BindView(R.id.zhdj_xw_imageview01)
    RoundImageView iv_xw_01;
    @BindView(R.id.zhdj_xw_imageview02)
    RoundImageView  iv_xw_02;


    Unbinder unbinder;
//    @BindView(R.id.zhdj_refreshLayout)
//    SmartRefreshLayout refreshLayout;

    @BindView(R.id.zhdj_listview)
    ListView listView;

    private ZhdjListViewAdapter listViewAdapter;
    List<ZhdjInfoBean> lstBannerBeans;
    List<ZhdjInfoBean> lstmiddleBeans;
    List<ZhdjInfoBean> lstDjxcBeans;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_dang_jian, container, false);
        unbinder = ButterKnife.bind(this, view);

        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }

        new PersionBiz().getZhdjInfo();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        new PersionBiz().getZhdjInfo();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.zhdj_top_imageview, R.id.zhdj_dyjy_imageview, R.id.zhdj_dygl_imageview,
            R.id.zhdj_dyfw_imageview, R.id.zhdj_dyfc_imageview, R.id.zhdj_xw_imageview01,
            R.id.zhdj_xw_imageview02})
    @Override
    public void onClick(View v) {
        //Log.d("Leon", "*************View:"+v.getId());
        ZhdjInfoBean zhdjInfoBean = null;
       // Map<String, String> params = new HashMap<>();
        switch (v.getId()) {
            case R.id.zhdj_top_imageview:
                zhdjInfoBean = lstBannerBeans.get(0);
                break;
            case R.id.zhdj_dyjy_imageview:
                Intent intent = new Intent(view.getContext(), MessageListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "25");
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);
                break;
            case R.id.zhdj_dygl_imageview:
                break;
            case R.id.zhdj_dyfw_imageview:
                break;
            case R.id.zhdj_dyfc_imageview:
                Intent intent_dyfc = new Intent(view.getContext(), MessageListActivity.class);
                Bundle bundle_dyfc = new Bundle();
                bundle_dyfc.putString("type", "26");
                intent_dyfc.putExtras(bundle_dyfc);
                view.getContext().startActivity(intent_dyfc);
                break;
            case R.id.zhdj_xw_imageview01:
                zhdjInfoBean = lstmiddleBeans.get(0);
                break;
            case R.id.zhdj_xw_imageview02:
                zhdjInfoBean = lstmiddleBeans.get(1);
                break;
                default:
        }

        if (zhdjInfoBean != null) {
            Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("content", zhdjInfoBean.getIm_content());
            bundle.putString("title", zhdjInfoBean.getIm_title());
            bundle.putString("img_url", zhdjInfoBean.getIm_imgPath());
            intent.putExtras(bundle);
            view.getContext().startActivity(intent);
        }

    }

    @Subscribe
    public void log(OldsEvent.ZhdjEvent msg) {
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        } else if (msg.what == 1) {
            updateView((ZhdjEntity) msg.obj);
        }
    }


    private void updateView(ZhdjEntity zhdjEntity) {
        lstBannerBeans = zhdjEntity.getData().getLstBanner();
        lstmiddleBeans = zhdjEntity.getData().getLstmiddle();
        lstDjxcBeans = zhdjEntity.getData().getLstDjxc();
        if (lstBannerBeans.get(0).getIm_imgPath() != null) {
            Glide.with(getContext()).load(lstBannerBeans.get(0).getIm_imgPath()).into(iv_top);
        }

        if (lstmiddleBeans.get(0).getIm_imgPath() != null) {
            Glide.with(getContext()).load(lstmiddleBeans.get(0).getIm_imgPath()).into(iv_xw_01);
        }

        if (lstmiddleBeans.get(1).getIm_imgPath() != null) {
            Glide.with(getContext()).load(lstmiddleBeans.get(1).getIm_imgPath()).into(iv_xw_02);
        }


        listViewAdapter = new ZhdjListViewAdapter(getContext(), lstDjxcBeans);
        listView.setAdapter(listViewAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ZhdjInfoBean zhdjInfoBean = lstDjxcBeans.get(position);
                Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", zhdjInfoBean.getIm_content());
                bundle.putString("title", zhdjInfoBean.getIm_title());
                bundle.putString("img_url", zhdjInfoBean.getIm_imgPath());
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);
            }
        });
    }
}
